package com.meixiang.view.filtermenu;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.util.DensityUtil;

/**
 * 筛选栏按钮
 *
 * @author LZRUI
 */
public class FilterMenuButton extends TextView {

    private boolean mIsChecked;
    private FilterMenuButtonClickListener filterMenuButtonClickListener;
    private Context mContext;

    public void setFilterMenuButtonClickListener(FilterMenuButtonClickListener filterMenuButtonClickListener) {
        this.filterMenuButtonClickListener = filterMenuButtonClickListener;
    }

    public FilterMenuButton(Context context) {
        this(context, null);
    }

    public FilterMenuButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FilterMenuButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        this.setChecked(false);
        setEvent();
    }

    private void setEvent() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setChecked(!mIsChecked);
                if (filterMenuButtonClickListener == null) {
                    throw new RuntimeException("FilterMenuButton 必须实现 FilterMenuButtonClickListener 接口,才能实现点击事件");
                }
                filterMenuButtonClickListener.onCheckedListener(mIsChecked);
            }
        });
    }

    /**
     * 设置是否选中
     *
     * @param isChecked
     */
    public void setChecked(boolean isChecked) {
        mIsChecked = isChecked;
        Drawable icon;
        if (isChecked) {
            // 被选中
            icon = ContextCompat.getDrawable(mContext, R.mipmap.unfold2);
        } else {
            // 未选中
            icon = ContextCompat.getDrawable(mContext, R.mipmap.unfold);
        }
        this.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);
        this.setPadding(DensityUtil.dip2px(mContext, 5), 0, DensityUtil.dip2px(mContext, 5), 0);
    }

    public interface FilterMenuButtonClickListener {
        void onCheckedListener(boolean isOpen);
    }
}
